package consume.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Argument;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Kevin
 * @date 2023/6/11 12:46
 * @description 监听死信队列
 **/
@Slf4j
@Component
public class DeadListener {

  /**
   * 监听死信消息
   *
   * @param msg
   */
  @RabbitListener(bindings = @QueueBinding(value = @Queue("dead-queue"),
          exchange = @Exchange(value = "dead-exchange", type = "direct"),
          key = {"dead"}))
  public void listener(String msg, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag,
                       Channel channel) throws Exception {

    // 手动签收
    channel.basicAck(deliveryTag, true);

    log.info("死信队列监听到消息--{}", msg);
  }

}
